package com.alex.wechat.mp.message.news.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.UploadFile;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDocument;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.pojo.base.TSTypegroup;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.alex.wechat.mp.config.WxContext;
import com.alex.wechat.mp.message.news.entity.WxNewsItem;
import com.alex.wechat.mp.message.news.entity.WxNewsTemplate;
import com.alex.wechat.mp.message.news.service.WxNewsItemServiceI;
import com.alex.wechat.mp.util.DateUtils;

/**
 * @author Debenson
 * @since 0.1
 */
@Controller
@RequestMapping("/wxArticleController")
public class WxArticleController extends BaseController {
  private static final Logger logger = Logger.getLogger(WxArticleController.class);

  @Autowired
  private WxNewsItemServiceI newsItemService;
  @Autowired
  private SystemService systemService;

  /**
   * 微信单图消息列表 页面跳转
   * 
   * @return
   */
  @RequestMapping(params = "goMessage")
  public ModelAndView goMessage(HttpServletRequest request) {
    String templateId = request.getParameter("templateId");
    // request.setAttribute("templateId", templateId);
    if (StringUtil.isNotEmpty(templateId)) {
      String hql = "from WxNewsItem where newsTemplate.id='" + templateId + "' order by orders asc";
      org.jeecgframework.core.util.LogUtil.info("...hql..." + hql);
      List<WxNewsItem> headerList = this.systemService.findByHql(hql);
      if (headerList.size() > 0) {
        request.setAttribute("headerNews", headerList.get(0));
        if (headerList.size() > 1) {
          ArrayList list = new ArrayList(headerList);
          list.remove(0);
          request.setAttribute("newsList", list);
        }
      }
      WxNewsTemplate newsTemplate = this.systemService.getEntity(WxNewsTemplate.class, templateId);
      Date addTime = newsTemplate.getCreateDate();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
      request.setAttribute("addtime", sdf.format(addTime));
    }
    return new ModelAndView("com/gomore/wechat/mp/message/news/showmessage");
  }

  /**
   * easyui AJAX请求数据
   * 
   * @param request
   * @param response
   * @param dataGrid
   * @param user
   */
  @RequestMapping(params = "datagrid")
  public void datagrid(WxNewsItem weixinArticle, HttpServletRequest request,
      HttpServletResponse response, DataGrid dataGrid) {
    CriteriaQuery cq = new CriteriaQuery(WxNewsItem.class, dataGrid);
    cq.eq("accountId", WxContext.getOriginalId());
    // 查询条件组装器
    org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, weixinArticle,
        request.getParameterMap());
    try {
      // 自定义追加查询条件
    } catch (Exception e) {
      throw new BusinessException(e.getMessage());
    }
    cq.add();
    this.newsItemService.getDataGridReturn(cq, true);
    TagUtil.datagrid(response, dataGrid);
  }

  /**
   * 删除微信单图消息
   * 
   * @return
   */
  @RequestMapping(params = "doDel")
  @ResponseBody
  public AjaxJson doDel(WxNewsItem weixinArticle, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    weixinArticle = systemService.getEntity(WxNewsItem.class, weixinArticle.getId());
    String message = "微信单图消息删除成功";
    try {
      newsItemService.delete(weixinArticle);
      systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
    } catch (Exception e) {
      e.printStackTrace();
      message = "微信单图消息删除失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  /**
   * 批量删除微信单图消息
   * 
   * @return
   */
  @RequestMapping(params = "doBatchDel")
  @ResponseBody
  public AjaxJson doBatchDel(String ids, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    String message = "微信单图消息删除成功";
    try {
      for (String id : ids.split(",")) {
        WxNewsItem weixinArticle = systemService.getEntity(WxNewsItem.class, id);
        newsItemService.delete(weixinArticle);
        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
      }
    } catch (Exception e) {
      logger.error("", e);
      j.setSuccess(false);
      message = "微信单图消息删除失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  /**
   * 添加微信单图消息
   * 
   * @param ids
   * @return
   */
  @RequestMapping(params = "doAdd")
  @ResponseBody
  public AjaxJson doAdd(WxNewsItem weixinArticle, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    String message = "微信单图消息添加成功";
    try {
      String templateId = request.getParameter("templateId");
      WxNewsTemplate temp1 = this.systemService.getEntity(WxNewsTemplate.class, templateId);
      weixinArticle.setNewsTemplate(temp1);
      if (WxContext.hasWxAccount()) {
        newsItemService.save(weixinArticle);
      } else {
        j.setSuccess(false);
        j.setMsg("请添加一个公众帐号。");
      }
      systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
    } catch (Exception e) {
      e.printStackTrace();
      message = "微信单图消息添加失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  /**
   * 更新微信单图消息
   * 
   * @param ids
   * @return
   */
  @RequestMapping(params = "doUpdate")
  @ResponseBody
  public AjaxJson doUpdate(WxNewsItem weixinArticle, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    String message = "微信单图消息更新成功";
    WxNewsItem t = newsItemService.get(WxNewsItem.class, weixinArticle.getId());
    try {
      MyBeanUtils.copyBeanNotNull2Bean(weixinArticle, t);
      newsItemService.saveOrUpdate(t);
      systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
    } catch (Exception e) {
      e.printStackTrace();
      message = "微信单图消息更新失败";
      throw new BusinessException(e.getMessage());
    }
    j.setMsg(message);
    return j;
  }

  /**
   * 微信单图消息新增页面跳转
   * 
   * @return
   */
  @RequestMapping(params = "goAdd")
  public ModelAndView goAdd(WxNewsItem weixinArticle, HttpServletRequest req) {
    String templateId = req.getParameter("templateId");
    req.setAttribute("templateId", templateId);
    if (StringUtil.isNotEmpty(weixinArticle.getId())) {
      weixinArticle = newsItemService.getEntity(WxNewsItem.class, weixinArticle.getId());
      req.setAttribute("weixinArticlePage", weixinArticle);
    }
    return new ModelAndView("com/gomore/wechat/mp/message/news/weixinArticle-add");
  }

  /**
   * 微信单图消息编辑页面跳转
   * 
   * @return
   */
  @RequestMapping(params = "goUpdate")
  public ModelAndView goUpdate(WxNewsItem weixinArticle, HttpServletRequest req) {
    if (StringUtil.isNotEmpty(weixinArticle.getId())) {
      weixinArticle = newsItemService.getEntity(WxNewsItem.class, weixinArticle.getId());
      req.setAttribute("weixinArticle", weixinArticle);
    }
    return new ModelAndView("com/gomore/wechat/mp/message/news/weixinArticle-update");
  }

  /**
   * 保存文件信息
   * 
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(params = "upload", method = RequestMethod.POST)
  @ResponseBody
  public AjaxJson upload(MultipartHttpServletRequest request, HttpServletResponse response) {
    AjaxJson j = new AjaxJson();
    Map<String, Object> attributes = new HashMap<String, Object>();
    TSTypegroup tsTypegroup = systemService.getTypeGroup("fieltype", "文档分类");
    TSType tsType = systemService.getType("files", "附件", tsTypegroup);
    String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID
    String documentTitle = oConvertUtils.getString(request.getParameter("documentTitle"));// 文件标题

    TSDocument document = new TSDocument();
    if (StringUtil.isNotEmpty(fileKey)) {
      document.setId(fileKey);
      document = systemService.getEntity(TSDocument.class, fileKey);
      document.setDocumentTitle(documentTitle);
    }
    document.setSubclassname(MyClassLoader.getPackPath(document));
    document.setCreatedate(DateUtils.getTimestamp());
    document.setTSType(tsType);
    UploadFile uploadFile = new UploadFile(request, document);
    // uploadFile.setCusPath("files");
    // uploadFile.setSwfpath("swfpath");
    document = systemService.uploadFile(uploadFile);

    attributes.put("url", document.getDownloadUrl());
    attributes.put("realPath", document.getRealpath());
    attributes.put("fileKey", document.getId());
    attributes.put("name", document.getAttachmenttitle());
    attributes.put("viewhref", "commonController.do?openViewFile&fileid=" + document.getId());
    attributes.put("delurl", "commonController.do?delObjFile&fileKey=" + document.getId());

    j.setMsg("文件添加成功");
    j.setAttributes(attributes);
    return j;
  }
}
